#!/usr/bin/env jq
# compile-config-1.00-qualified_names -- Normalizes config to use qualified names
#
# - extractor names are prefixed with process/*
# - factor names are prefixed with factor/*
# - output_relation names are prefixed with data/* and kept under a new key output_
# - dependencies are rewritten with qualified names under a new key dependencies_
##
.deepdive_ as $deepdive

# TODO add default fields to all extractors and factors

# qualify extractor names and output relations
| .deepdive_.extraction.extractors |= with_entries
    ( .key |= "process/\(.)"
    | .value |= ( .dependencies_ = .dependencies
                | .dependencies_ |= (. // [] | map("process/\(.)"))
                | .input_ = (.input_relations // [] | map("data/\(.)"))
                | .output_ = (.output_relation | if . then ["data/\(.)"] else null end)
                )
    )

# qualify factor names
| .deepdive_.inference.factors |= with_entries
    ( .key |= "factor/\(.)"
    | .value |= ( .dependencies_ = .dependencies
                | .dependencies_ |= (. // [] | map("process/\(.)"))
                | .input_ = (.input_relations // [] | map("data/\(.)"))
                | .output_ = (.output_relation | if . then ["data/\(.)"] else null end)
                )
    )

# turn all extractors into processes in the execution plan under compilation
| .deepdive_.execution.processes += .deepdive_.extraction.extractors
